﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using IPMsgExpections;
using CommonUtility;

namespace MessageCenter
{
    public class IPMsgFileInfo
    {
        private string path;
        private string name;
        private long fileSize;
        private DateTime crTime;
        private IPMsgFileTransferMode transferMode;
        private IPMsgFileMode fileMode;
        private FileInfo fileInfo;

        public string Path
        {
            get { return this.path; }
            set { this.path = value; }
        }
        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }
        public long FileSize
        {
            get { return this.fileSize; }
            set { this.fileSize = value; }
        }
        public DateTime CrTime
        {
            get { return this.crTime; }
            set { this.crTime = value; }
        }
        public IPMsgFileTransferMode TransferMode
        {
            get { return this.transferMode; }
            set { this.transferMode = value; }
        }
        public IPMsgFileMode FileMode
        {
            get { return this.fileMode; }
            set { this.fileMode = value; }
        }
        public FileInfo FileInfo
        {
            get { return this.fileInfo; }
            set { this.fileInfo = value; }
        }

        public IPMsgFileInfo(string filePath)
        {
            filePath = filePath.TrimEnd('\\');
            FileInfo fileInfo = new FileInfo(filePath);
            this.fileInfo = fileInfo;

            if (fileInfo.Attributes == (FileAttributes)(-1))
            {
                throw new FileExpection("文件或者文件夹不存在.");
            }

            if (fileInfo.Attributes == FileAttributes.Directory)
            {
                this.fileMode = IPMsgFileMode.Directory;
                this.fileSize = 0;
            }
            else
            {
                this.fileMode = IPMsgFileMode.File;
                this.fileSize = fileInfo.Length;
            }

            this.path = filePath;
            this.name = fileInfo.Name;
            this.crTime = fileInfo.CreationTime;
            this.transferMode = IPMsgFileTransferMode.Put;
        }

        public string ConvertToAttachMessage()
        {
            uint indexFile = 0;
            string retMessage = indexFile.ToString()
                + ConstWords.IPMSG_SEPARATOR
                + this.name
                + ConstWords.IPMSG_SEPARATOR
                + this.fileSize.ToString("X4")
                + ConstWords.IPMSG_SEPARATOR
                + ((uint)UnixTimeConverter.ToUnixTime(this.crTime)).ToString("X4")
                + ConstWords.IPMSG_SEPARATOR
                + ((int)this.fileMode).ToString()
                + ConstWords.IPMSG_ENDOFMSG;

            return retMessage;
        }
    }

    public enum IPMsgFileTransferMode
    {
        Put,
        Get
    }

    public enum IPMsgFileMode : uint
    {
        File = ConstWords.FileTypes.IPMSG_FILE_REGULAR,
        Directory = ConstWords.FileTypes.IPMSG_FILE_DIR,
        RetParent = ConstWords.FileTypes.IPMSG_FILE_RETPARENT
    }
}
